/*
 * Copyright 2023-2025 Licensed under the Dual Licensing
 * website: https://aizuda.com
 */
package com.aizuda.bpm.beetlsql.impl;

import com.aizuda.bpm.beetlsql.mapper.FlwTaskMapper;
import com.aizuda.bpm.engine.dao.FlwTaskDao;
import com.aizuda.bpm.engine.entity.FlwTask;
import com.github.yitter.idgen.YitIdHelper;
import org.beetl.sql.core.query.LambdaQuery;

import java.util.Date;
import java.util.List;

/**
 * 任务数据访问层接口实现类
 *
 * <p>
 * <a href="https://aizuda.com">官网</a>尊重知识产权，不允许非法使用，后果自负，不允许非法使用，后果自负
 * </p>
 *
 * @author hubin
 * @since 1.0
 */
public class FlwTaskDaoImpl implements FlwTaskDao {
    private final FlwTaskMapper taskMapper;

    public FlwTaskDaoImpl(FlwTaskMapper taskMapper) {
        this.taskMapper = taskMapper;
    }

    @Override
    public boolean insert(FlwTask flwTask) {
        flwTask.setId(YitIdHelper.nextId());
        return taskMapper.createQuery().insertSelective(flwTask) > 0;
    }

    @Override
    public boolean deleteById(Long id) {
        return taskMapper.deleteById(id) > 0;
    }

    @Override
    public boolean deleteByInstanceIdAndParentTaskId(Long instanceId, Long parentTaskId) {
        return taskMapper.createLambdaQuery().andEq(FlwTask::getInstanceId, instanceId)
                        .andEq(FlwTask::getParentTaskId, parentTaskId).delete()>0;
    }

    @Override
    public boolean deleteByInstanceIds(List<Long> instanceIds) {
        return taskMapper.createLambdaQuery().andIn(FlwTask::getInstanceId, instanceIds).delete()>0;
    }

    @Override
    public boolean deleteByIds(List<Long> ids) {
        return taskMapper.createLambdaQuery().andIn(FlwTask::getId, ids).delete()>0;
    }

    @Override
    public boolean updateById(FlwTask flwTask) {
        return taskMapper.createLambdaQuery().andEq(FlwTask::getId, flwTask.getId()).updateSelective(flwTask)>0;
    }

    @Override
    public FlwTask selectById(Long id) {
        return taskMapper.single(id);
    }

    @Override
    public Long selectCountByParentTaskId(Long parentTaskId) {
        return taskMapper.createLambdaQuery().andEq(FlwTask::getParentTaskId, parentTaskId).count();
    }

    @Override
    public Long selectCountByInstanceId(Long instanceId) {
        return taskMapper.createLambdaQuery().andEq(FlwTask::getInstanceId, instanceId).count();
    }

    @Override
    public List<FlwTask> selectListByInstanceId(Long instanceId) {
        return taskMapper.createLambdaQuery().andEq(FlwTask::getInstanceId, instanceId).select();
    }

    @Override
    public List<FlwTask> selectListByInstanceIdAndTaskName(Long instanceId, String taskName) {
        return taskMapper.createLambdaQuery().andEq(FlwTask::getInstanceId, instanceId)
                .andEq(FlwTask::getTaskName, taskName).select();
    }

    @Override
    public List<FlwTask> selectListByInstanceIdAndTaskKey(Long instanceId, String taskKey) {
        return taskMapper.createLambdaQuery().andEq(FlwTask::getInstanceId, instanceId)
                .andEq(FlwTask::getTaskKey, taskKey).select();
    }

    @Override
    public List<FlwTask> selectListByInstanceIdAndTaskNames(Long instanceId, List<String> taskNames) {
        return taskMapper.createLambdaQuery().andEq(FlwTask::getInstanceId, instanceId)
                .andIn(FlwTask::getTaskName, taskNames).select();
    }

    @Override
    public List<FlwTask> selectListTimeoutOrRemindTasks(Date currentDate) {
        LambdaQuery<FlwTask> query = taskMapper.createLambdaQuery();
        return query.andLessEq(FlwTask::getExpireTime, currentDate)
                .or(query.condition().andLessEq(FlwTask::getRemindTime, currentDate).andGreat(FlwTask::getRemindRepeat, 0))
                .select();
    }

    @Override
    public List<FlwTask> selectListByParentTaskId(Long parentTaskId) {
        return taskMapper.createLambdaQuery().andEq(FlwTask::getParentTaskId, parentTaskId).select();
    }

    @Override
    public List<FlwTask> selectListByParentTaskIds(List<Long> parentTaskIds) {
        return taskMapper.createLambdaQuery().andIn(FlwTask::getParentTaskId, parentTaskIds).select();
    }
}
